import logging
from path import *
from config import args

level_relations = {
    'NOTSET': logging.NOTSET,
    'DEBUG': logging.DEBUG,
    'INFO': logging.INFO,
    'WARNING': logging.WARNING,
    'ERROR': logging.ERROR,
    'CRITICAL': logging.CRITICAL
}


class Logger:
    def __init__(self, filename=LOGGING_FILE, level=args.logging_level,
                 fmt='%(asctime)s - %(levelname)s: %(message)s'):
        self.filename = filename
        self.fmt = fmt
        self.mkdir()

        self.logger = logging.getLogger(filename)
        self.logger.setLevel(level_relations[level])
        self.set_logger()

    def mkdir(self):
        logging_dir = os.path.dirname(self.filename)
        if not os.path.exists(logging_dir):
            os.makedirs(logging_dir)

    def set_logger(self):
        sh = logging.StreamHandler()
        format_str = logging.Formatter(self.fmt)
        sh.setFormatter(format_str)
        sh.setLevel(level_relations[args.print_log_level])

        fh = logging.FileHandler(self.filename)
        fh.setFormatter(format_str)

        self.logger.addHandler(sh)
        self.logger.addHandler(fh)


logger = Logger()


if __name__ == '__main__':
    log = Logger()
    log.logger.debug('debug')
    log.logger.info('info')
    log.logger.warning('警告')
    log.logger.error('报错')
    log.logger.critical('严重')
